const api = require("../../../../utils/cloudAPIUtils");
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        deviceSn:null,
        devInfo:{},
        agreement:{
            checked:true
        },
        useCommon:false,
        usePrePay:false
    },

    btnPay:function(){
        const self = this;
        if(!this.data.agreement.checked){
            wx.showModal({
              title: '租借失败',
              content: '请先勾选同意授权书',
              showCancel:false,
            })
            return
        }
        api.post(api.url.cdbPay,{
            payType:3,
            appId:wx.getAccountInfoSync().miniProgram.appId,
            deviceSn:this.data.deviceSn,
            costMode:this.data.devInfo.costMode
        },(res)=>{
            console.log(res)
            if(res.data.code==200){
            const tradeNo = res.data.body.tradeNo;

            const payInfo = JSON.parse(res.data.body.payInfo);
            wx.requestPayment({
                appId:payInfo.appId,
                nonceStr: payInfo.nonceStr,
                package: payInfo.package,
                paySign: payInfo.sign,
                timeStamp: payInfo.timeStamp+"",
                signType:payInfo.signType,
                success(res){
                    console.log(res)
                    self.toUnderway(tradeNo);
                },fail(err){
                    wx.showToast({
                        title: '支付失败',
                        icon:'error',
                        mask:true
                    })
                }
            })
            }else{
                wx.showModal({
                    title: '支付失败',
                    content: res.data.msg,
                    showCancel:false,
                    complete: () => {
                    wx.reLaunch({
                        url: '/pages/newIndex/index',
                    })
                    }
                })
            }
        })
    },

    btnPrePay:function(){
        const self = this;
        if(!this.data.agreement.checked){
            wx.showModal({
              title: '租借失败',
              content: '请先勾选同意授权书',
              showCancel:false,
            })
            return
        }
        api.post(api.url.cdbPay,{
            payType:3,
            appId:wx.getAccountInfoSync().miniProgram.appId,
            deviceSn:this.data.deviceSn,
            costMode:this.data.devInfo.costMode
        },(res)=>{
            console.log(res)
            if(res.data.code==200){
                const tradeNo = res.data.body.tradeNo;
                const payInfo = JSON.parse(res.data.body.payInfo);
                wx.openBusinessView({
                    businessType: 'wxpayScoreUse',
                    extraData: {
                        mch_id: payInfo.mch_id,
                        package: payInfo.package,
                        timestamp: payInfo.timestamp + "",
                        nonce_str: payInfo.nonce_str,
                        sign_type: 'HMAC-SHA256',
                        sign: payInfo.sign
                    },
                    envVersion: 'release',
                    success(res) {
                        console.log(res)
                        if (res.errCode == 0 && res.extraData.appid) {
                            self.toUnderway(tradeNo);
                        } else {
                            self.setData({
                                usePrePay:false
                            })
                        }
                    },
                    fail(err) {
                        console.log(err);
                        self.setData({
                            usePrePay:false
                        })
                    },
                });
            }else{
                wx.showModal({
                    title: '支付失败',
                    content: res.data.msg,
                    showCancel:false,
                    complete: () => {
                        wx.reLaunch({
                            url: '/pages/index/index',
                        })
                    }
                })
            }
        })
    },

    toUnderway(tradeNo){
        console.log(tradeNo);
        wx.redirectTo({
          url: '../result/result?tradeNo='+tradeNo,
        })
    },

    toIndex(){
        wx.reLaunch({
          url: '/pages/index/index',
        })
    },

    //勾选授权
    btnChangeRadio1(){
        const agreement = this.data.agreement;
        agreement.checked = !agreement.checked;
        this.setData({agreement});
    },

    btnSettingUse(){
        const self = this;
        wx.showModal({
          title: '常用设备',
          content: '设置为常用设备？',
          complete: (res) => {
            if (res.confirm) {
              api.post(api.url.useDeviceAdd,{
                  deviceSn:self.data.deviceSn
                },res=>{
                    if(res.data.code==200){
                      wx.showToast({title: '设置成功！',icon:'none'})
                      self.setData({useCommon:false})
                    }else{
                      wx.showToast({title: res.data.msg,icon:'error'})
                    }
                })
            }
          }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if(options.deviceSn){
            this.setData({
                deviceSn:options.deviceSn,
                useCommon:options.use==1?true:false
            })
        }
        api.post(api.url.cdbScan,{
            deviceSn:options.deviceSn
        },res=>{
            if(res.data.code==200){
                this.setData({
                    devInfo:res.data.body,
                    usePrePay:res.data.body.costMode == 2?true:false
                })
                if(res.data.body.state == 2){
                    wx.showModal({
                        title: '温馨提示',
                        content: '设备离线',
                        showCancel:false,
                        success(){
                            wx.reLaunch({
                               url: '/pages/index/index',
                             })
                        }
                    })
                }
            }else{
                wx.showModal({
                  content: res.data.msg,
                  showCancel:false,
                  success(){
                      wx.reLaunch({
                        url: '/pages/index/index',
                      })
                  }
                })
            }
        })
    }

})